package com.yc.czxy.utils;

import com.yc.czxy.constant.StatusCode;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * 返回数据
 * @program: chengzixy1
 * @description:
 * @author:
 * @create: 2021-09-06 10:56
 */
@Accessors(chain = true)
@Data
@ApiModel("统一响应格式类")
public class R<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty("状态码，如无特别说明，0为成功的请求")
    private int code;
    @ApiModelProperty("响应信息")
    private String msg;
    @ApiModelProperty("响应数据")
    private T result;

    public static <T> R<T> ok() {
        return restResult(null, StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMsg());
    }

    public static <T> R<T> ok(T data) {
        return restResult(data, StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMsg());
    }

    public static <T> R<T> ok(T data, String msg) {
        return restResult(data, StatusCode.SUCCESS.getCode(), msg);
    }

    public static <T> R<T> ok(int code, String msg) {
        return restResult(null, code, msg);
    }

    public static <T> R<T> error() {
        return restResult(null, StatusCode.FAILURE.getCode(), StatusCode.FAILURE.getMsg());
    }

    public static <T> R<T> error(String msg) {
        return restResult(null, StatusCode.FAILURE.getCode(), msg);
    }

    public static <T> R<T> error(T data) {
        return restResult(data, StatusCode.FAILURE.getCode(), StatusCode.FAILURE.getMsg());
    }

    public static <T> R<T> error(int code, String msg) {
        return restResult(null, code, msg);
    }

    public static <T> R<T> error(T data, String msg) {
        return restResult(null, StatusCode.FAILURE.getCode(), msg);
    }

    public static <T> R<T> result(StatusCode statusCode) {
        return restResult(null, statusCode.getCode(), statusCode.getMsg());
    }

    public static <T> R<T> restResult(T data, int code, String msg) {
        R<T> apiResult = new R<T>();
        apiResult.setCode(code);
        apiResult.setResult(data);
        apiResult.setMsg(msg);
        return apiResult;
    }
}
